package com.design.graduation.asdemo.utils;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.design.graduation.asdemo.base.BaseActivity;
import com.design.graduation.asdemo.model.InfoEntity;

import java.text.ParseException;
import java.util.List;

/**
 * Created by arvin on 2016/1/18 11:54.
 */
public class SharePreferenceUtils {
    private static final String INFO = "info";
    private static final String LAST_LOCATION = "last_location";

    private static SharedPreferences getPreferences() {
        return PreferenceManager.getDefaultSharedPreferences(BaseActivity.getInstance());
    }

    public static void saveInfo(List<InfoEntity> infoEntities) {
        getPreferences().edit().putString(INFO, CurrentUtils.getJsonFromObject(infoEntities)).commit();
    }

    public static List<InfoEntity> getInfo() throws ParseException {
        return CurrentUtils.getInfoListFromJson(getPreferences().getString(INFO, "[]"));
    }

    public static void saveLastLocation(InfoEntity infoEntity) {
        getPreferences().edit().putString(LAST_LOCATION, CurrentUtils.getJsonFromObject(infoEntity)).commit();
    }

    public static InfoEntity getLastLocation() throws ParseException {
        return CurrentUtils.getInfoEntityFromJson(getPreferences().getString(LAST_LOCATION, ""));
    }


}
